<template>
<div>
  <div class="wrap">
    <div class="form-inner">
      <div class="form-group">
        <input type="text" class="form-control" v-model="title" >
      </div>
      <div class="form-group f-cb">
        <div class="img-wrap f-fl">
          <img :src="icon | realImgSrc">
          <a class="change-icon" href="javascript:;" @click="changePic">更换封面</a>
        </div>
        <div class="text-wrap f-fr"> 
          <textarea class="form-control" rows="3" v-model="description"></textarea>
        </div>
      </div>
      <div class="form-group select-wrap" style="display:flex;">
        <div class="left">
          <h2>模版分类</h2>
          <div class="scroller">
            <dl v-for="item in category">
              <dt @click="hidelist(item)"> 
                <span class="i-wrap down">
                  <i :class="{'icon-angle-down': item.toggle, 'icon-angle-right': !item.toggle}"></i>
                </span>
                {{item.name}}
              </dt>
              <template v-if="item.toggle">
                <dd 
                  v-for="tag in item.tags"
                  @click="selectTag(tag)"
                  >
                  {{tag.name}}
                </dd>                
              </template>
            </dl>
          </div>      
        </div>
        <div class="center" style="">
          <i class="icon-angle-right"></i>
        </div>
        <div class="right">
          <h2>已选分类</h2>
          <ul>
            <li v-for="(index, tag) in seletedTags">
              {{tagArr | filterTag tag}}
              <a href="javascript:;" class="btn-del" @click="removeTag(tag)">&times;</a>
            </li>
          </ul>
        </div>
      </div>
      <div class="form-group">
        <div class="btn btn-warning btn-save" @click="save">保存</div>
      </div>
    </div>
  </div>
</div>
</template>
<script>
var category = [{
  name: '场景',
  tags: [{
    name: '互联网',
    id: 1
  }, {
    name: '电商',
    id: 2
  }, {
    name: '服装',
    id: 3
  }, {
    name: '游戏',
    id: 4
  }]
}, {
  name: '行业',
  tags: [{
    name: ' IT',
    id: 5
  }, {
    name: 'IT01',
    id: 6
  }]
}, {
  name: '热点',
  tags: [{
    name: '热点01',
    id: 7
  }, {
    name: '热点02',
    id: 8
  }, {
    name: '热点03',
    id: 9
  }]
}]; 
export default {
  props: {
    title : {
      type: String,
      require: true,
      twoway: true
    },
    description : {
      type: String,
      require: true,
      twoway: true
    },
    icon : {
      type: [String , Number],
      require: true,
      twoway: true
    },
    tags : {
      type: [String],
      require: true,
      twoway: true,
      default: ""
    },    
    show : {
      type: Boolean,
      require: true,
      twoway: true
    }        
  },
  data: function(){
    return {
      showPictureDialog: false,
      category:[],
      tagArr: []
    }
  },
  computed: {
    seletedTags: {
      get: function(){
        if(this.tags){
          return this.tags.split(',');
        }
        else{
          return [];
        }
      },
      set: function(val){
        this.tags = val.join(',');
      }
    }
  },
  filters: {
    filterTag: function(tagArr, tagid){ 
      var tag = tagArr.filter(function(item){
        return item.id == tagid;
      })[0];
      return tag && tag.name || "";
    }
  },
  created: function(){
    if( process.env.NODE_ENV !== 'production'){
      this.category = category.map(function(item){
        item.toggle = true;
        return item;
      });
      this.tagArr = this.category.reduce(function(a, b) {
        return a.concat(b.tags);
      }, []);
    }
    else {
      // this.loadTags();
    }
  },
  methods: {
    loadTags: function(){
      var _this = this;
      $.ajax({
        url: Global.api.getTag,
        method: "GET",
        dataType: "json"
      }).success(function (result) {
        if (result.type == "success") {
          var data = result.data.map(function(item) {
            item.toggle = true;
            return item;
          });
          _this.category = data;
          _this.tagArr = _this.category.reduce(function(a, b) {
            return a.concat(b.tags);
          }, []);          
        } else {
          layer.alert("获取模版tag失败")
        }        
      }).error(function(){
        console.log('获取模版tag失败', result);
      });
    },    
    selectTag: function(tag){
      if( this.seletedTags.indexOf(String(tag.id)) == -1 ){
        this.seletedTags = this.seletedTags.concat([tag.id]);
      }
    },
    removeTag: function(tag){
      this.seletedTags = this.seletedTags.filter(function(item){
        return item != tag;
      });
    },  
    hidelist: function(item){
      item.toggle = !item.toggle;
    },   
    changePic: function(){
      this.$dispatch("showPictureDialog", "changeAppIcon");
    },
    save: function(){
      this.show = false;
    }
  },
  components : {
    pictureDialog : require('./plugin/pictureDialog.vue'),
  }    
}
</script>
<style scoped>
  .wrap{
    overflow: hidden;
    padding: 10px;
  }
  .img-wrap{
    display: inline-block;
    position: relative;
    width:100px;
    height:100px;
    border:1px solid #ccc;
  }
  .img-wrap img{
    width: 100%;
    height:100%;
  }
  .img-wrap:hover .change-icon{
    display: block;
  }
  .change-icon {
    position: absolute;
    display: none;
    color: #fff;
    left: 0;
    bottom: 0;
    width: 100%;
    line-height: 30px;
    text-align: center;
    text-decoration: none;
    background: #4c4c4c;
  }
  .text-wrap {
    height: 100px;
    width:80%;
  }
  .text-wrap textarea{
    width:100%;
    height:100%;
  }
  .select-wrap {
    display: flex;
    overflow: hidden;
  }
  .left, .right {
    flex: 1;
  }
  .left h2, .right h2{
    font-size: 14px;
    color: #999;
    margin-bottom: 10px;
  }  
  .scroller, ul{
    overflow:auto;
    height: 200px;
    border: 1px solid #ddd;
  }
  .i-wrap{
    display: inline-block;
    width: 16px;
    text-align: center;
    font-size: 18px;
    color: #ff9901;    
  } 
  dt, dd{
    line-height: 30px;
    cursor: pointer;
  }
  dd{
    margin: 0;
    padding-left: 30px;
  }
  dt:hover,
  dd:hover{
    background-color: rgb(255, 238, 213);
  }
  .center{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 60px; 
    text-align:center;
    font-size:30px;
  }
  .center i {
    transform: scaleY(2);
    color: #bbb;  
  }
li{
  line-height: 30px;
  padding-left: 15px;
  cursor: pointer;
}
li:nth-child(odd){
  background: #eee;
}
li:hover{
  background-color: rgb(255, 238, 213);
}
li:hover .btn-del{
  display: block;
}
li .btn-del {
  display: none;
  float: right;
  width:30px;
  height:30px;
  text-align: center;
  color: rgb(255, 153, 1);
  text-decoration: none;
}
li .btn-del:hover{
  background-color: rgb(255, 215, 156);
}
.btn-save{
  float:right;
}
</style>